
import java.util.Scanner;
public class Queen {
static int N=8;
static int count=0;
static int[] position=new int[N];
 
 
static void outPut() {
int i,j;
System.out.println("第"+(++count)+"种方法输出:");
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
if(position[i]==j) {
System.out.print("Q");
}
else {
System.out.print("*");
}
}
System.out.println();
}
}
static void eightQueen(int n) {
int i,j;
int ct;
if(n==N) {
outPut();
return;
}
for(i=0;i<N;i++) {
position[n]=i;
ct=1;
for(j=0;j<n;j++) {
if(position[n]==position[j]||Math.abs(position[j]-position[n])==(n-j))
ct=0;
}
if(ct==1)
eightQueen(n+1);
}
}
 
 
public static void main(String[] args) {
System.out.println("请输入皇后N的个数:");
Scanner scanner=new Scanner(System.in);
N=scanner.nextInt();
System.out.println(N+"皇后的放置方法有:");
eightQueen(0);
}
}
